package io.xccit.listener;

import io.xccit.service.IProductService;
import io.xccit.to.OrderToProduct;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author CH_ywx
 * @date 2023-09-21
 * @description 商品服务MQ消息监听
 */
@Component
public class ProductMQListener {

    @Autowired
    private IProductService productService;

    /**
     * 根据订单内容查询对应商品并减库存
     * @param orderToProducts
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "sub.queue"),
                    exchange = @Exchange(value = "topic.ex"),
                    key = {"sub.number"}
            )
    )
    public void updateProductNum(List<OrderToProduct> orderToProducts){
        productService.updateProductNum(orderToProducts);
    }
}
